﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.AssetManage
{
    public partial class AssetAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
             
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup=" + (int)PubEnums.RoleGroup.客户), ddlCompanyID, "ID", "CompanyName");//绑定公司
                if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.客户.ToString())
                {
                    ddlCompanyID.SelectedValue = SessionHelper.UserCompanyID.ToString();
                    ddlCompanyID.Enabled = false;
                }

                DropDownListHelper.InitDropList<tbAssetClassEntity>(new tbAssetClassEntity().GetAll(), ddlAssetClass, "ID", "ClassName");//绑定资产类别
                DropDownListHelper.InitDropList<tbAssetStateEntity>(new tbAssetStateEntity().GetAll(), ddlAssetState, "ID", "StateName");//绑定资产状态
                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        titleInfo.InnerText = "资产编辑";
                        DataShow(pid);
                        ddlAssetClass_SelectedIndexChanged(null, null);
                        this.ddlCompanyID.Enabled = false;
                        this.ddlAssetClass.Enabled = false;
                    }
                }

                if (Request.Params["sid"] != null)
                {
                    string sid = Request.Params["sid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        titleInfo.InnerText = "资产查看";
                        DataShow(pid);
                        ddlAssetClass_SelectedIndexChanged(null, null);
                        this.btnSave.Visible = false;
                        this.ddlCompanyID.Enabled = false;
                        this.ddlAssetClass.Enabled = false;
                    }
                }
            }
        }


        private void DataShow(int troubleId)
        {
            tbAssetEntity entity = new tbAssetEntity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;
                ddlCompanyID.SelectedValue = entity.customerID.ToString();
                txtAssetNo.Text=entity.AssetNo ;//资产编号
                txtAssetName.Text=entity.AssetName ;//资产名称
                txtAssetDescription.Text=entity.AssetDescription;//资产描述
                ddlAssetState.Items.FindByValue(entity.AssetState.ToString()).Selected = true;//资产状态
                ddlAssetClass.Items.FindByValue(entity.ClassID.ToString()).Selected = true;//资产类别
                txtInvoiceNo.Text=entity.InvoiceNo;//发票号码
                txtLinkMan.Text=entity.LinkMan;//联系人
                txtMadeName.Text = entity.MadeName;//制造商
                txtPrincipal.Text=entity.Principal;//负责人
                txtSaler.Text=entity.SalerName;//销售商
                txtPurchaseDate.Text=entity.PurchaseDate.ToString();//购买日期
                txtAssetVocation.Text=entity.AssetVocation;//资产位置
                txtAssetUser.Text=entity.AssetUser;//使用人
                txtSerialNumber.Text = entity.Serial_number;//序列号
                txtAssetModel.Text=entity.AssetModel; //资产型号

                if (ddlAssetClass.SelectedValue == PubEnums.AssetType.软件资产列表.ToString("D"))
                {
                    txtT_Row_18_1.Text = entity.Expand1;
                    txtT_Row_18_2.Text = entity.Expand2;
                    txtT_Row_18_3.Text = entity.Expand3;
                    txtT_Row_18_4.Text = entity.Expand4;
                    txtT_Row_18_5.Text = entity.Expand5;
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.办公设备资产.ToString("D"))
                {
                    txtT_Row_2_1.Text = entity.Expand1;
                    txtT_Row_2_2.Text = entity.Expand2;
                    txtT_Row_2_3.Text = entity.Expand3;
                    txtT_Row_2_4.Text = entity.Expand4;
                    txtT_Row_2_5.Text = entity.Expand5;
                    txtT_Row_2_6.Text = entity.Expand6;
                    txtT_Row_2_7.Text = entity.Expand7;
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.电脑资产列表.ToString("D"))
                {
                    txtT_Row_5_1.Text = entity.Expand1;
                    txtT_Row_5_2.Text = entity.Expand2;
                    txtT_Row_5_3.Text = entity.Expand3;
                    txtT_Row_5_4.Text = entity.Expand4;
                    txtT_Row_5_5.Text = entity.Expand5;
                    txtT_Row_5_6.Text = entity.Expand6;
                    txtT_Row_5_7.Text = entity.Expand7;
                    txtT_Row_5_8.Text = entity.Expand8;
                    txtT_Row_5_9.Text = entity.Expand9;
                    txtT_Row_5_10.Text = entity.Expand10;
                    txtT_Row_5_11.Text = entity.Expand11;
                    txtT_Row_5_12.Text = entity.Expand12;
                    txtT_Row_5_13.Text = entity.Expand13;
                    txtT_Row_5_14.Text = entity.Expand14;
                    txtT_Row_5_15.Text = entity.Expand15;
                    txtT_Row_5_16.Text = entity.Expand16;
                    txtT_Row_5_17.Text = entity.Expand17;
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.关键资产列表.ToString("D"))
                {
                    txtT_Row_5_18.Text = entity.Expand1;
                    txtT_Row_5_19.Text = entity.Expand2;
                    txtT_Row_5_20.Text = entity.Expand3;
                    txtT_Row_5_21.Text = entity.Expand4;
                    txtT_Row_5_22.Text = entity.Expand5;
                    txtT_Row_5_23.Text = entity.Expand6;
                    txtT_Row_5_24.Text = entity.Expand7;
                    txtT_Row_5_25.Text = entity.Expand8;
                    txtT_Row_5_26.Text = entity.Expand9;
                    txtT_Row_5_27.Text = entity.Expand10;
                    txtT_Row_5_28.Text = entity.Expand11;
                    txtT_Row_5_29.Text = entity.Expand12;
                    txtT_Row_5_30.Text = entity.Expand13;
                    txtT_Row_5_31.Text = entity.Expand14;
                    txtT_Row_5_32.Text = entity.Expand15;
                }
                else if (this.ddlAssetClass.SelectedValue == PubEnums.AssetType.其他资产列表.ToString("D"))
                {
                    txtT_Row_11_1.Text = entity.Expand1;
                    txtT_Row_11_2.Text = entity.Expand2;
                    txtT_Row_11_3.Text = entity.Expand3;
                    txtT_Row_11_4.Text = entity.Expand4;
                    txtT_Row_11_5.Text = entity.Expand5;
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.网络设备资产.ToString("D"))
                {
                    txtT_Row_20_1.Text = entity.Expand1;
                    txtT_Row_20_2.Text = entity.Expand2;
                    txtT_Row_20_3.Text = entity.Expand3;
                    txtT_Row_20_4.Text = entity.Expand4;
                    txtT_Row_20_5.Text = entity.Expand5;
                    txtT_Row_20_6.Text = entity.Expand5;
                    txtT_Row_20_7.Text = entity.Expand5;
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbAssetEntity entity = new ITOMMS.Entity.tbAssetEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbAssetEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }

                //entity.customerID = SessionHelper.UserCompanyID;
                entity.customerID = int.Parse(ddlCompanyID.SelectedValue);//所属公司

               
                entity.AssetNo = txtAssetNo.Text.Trim();//资产编号
                entity.AssetName = txtAssetName.Text.Trim();//资产名称
                entity.AssetDescription = txtAssetDescription.Text.Trim();//资产描述
                entity.AssetState = int.Parse(ddlAssetState.SelectedValue);//资产状态
                entity.ClassID = int.Parse(ddlAssetClass.SelectedValue);//资产类别
                entity.InvoiceNo = txtInvoiceNo.Text.Trim();//发票号码
                entity.LinkMan = txtLinkMan.Text.Trim();//联系人
                entity.MadeName = txtMadeName.Text.Trim();//制造商
                entity.Principal = txtPrincipal.Text.Trim();//负责人
                entity.SalerName = txtSaler.Text.Trim();//销售商
                entity.Serial_number = txtSerialNumber.Text.Trim(); //序列号
                entity.AssetModel = txtAssetModel.Text.Trim(); //资产型号

                if (txtPurchaseDate.Text.Trim()!="")
                    entity.PurchaseDate = DateTime.Parse(txtPurchaseDate.Text.ToString());//购买日期
               
                entity.AssetVocation = txtAssetVocation.Text.Trim();//资产位置
                entity.AssetUser = txtAssetUser.Text.Trim();//使用人


                if (ddlAssetClass.SelectedValue == PubEnums.AssetType.软件资产列表.ToString("D"))
                {
                    entity.Expand1 = txtT_Row_18_1.Text.Trim();
                    entity.Expand2 = txtT_Row_18_2.Text.Trim();
                    entity.Expand3 = txtT_Row_18_3.Text.Trim();
                    entity.Expand4 = txtT_Row_18_4.Text.Trim();
                    entity.Expand5 = txtT_Row_18_5.Text.Trim();
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.办公设备资产.ToString("D"))
                {
                    entity.Expand1 = txtT_Row_2_1.Text.Trim();
                    entity.Expand2 = txtT_Row_2_2.Text.Trim();
                    entity.Expand3 = txtT_Row_2_3.Text.Trim();
                    entity.Expand4 = txtT_Row_2_4.Text.Trim();
                    entity.Expand5 = txtT_Row_2_5.Text.Trim();
                    entity.Expand6 = txtT_Row_2_6.Text.Trim();
                    entity.Expand7 = txtT_Row_2_7.Text.Trim();
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.电脑资产列表.ToString("D"))
                {
                    entity.Expand1 = txtT_Row_5_1.Text.Trim();
                    entity.Expand2 = txtT_Row_5_2.Text.Trim();
                    entity.Expand3 = txtT_Row_5_3.Text.Trim();
                    entity.Expand4 = txtT_Row_5_4.Text.Trim();
                    entity.Expand5 = txtT_Row_5_5.Text.Trim();
                    entity.Expand6 = txtT_Row_5_6.Text.Trim();
                    entity.Expand7 = txtT_Row_5_7.Text.Trim();
                    entity.Expand8 = txtT_Row_5_8.Text.Trim();
                    entity.Expand9 = txtT_Row_5_9.Text.Trim();
                    entity.Expand10 = txtT_Row_5_10.Text.Trim();
                    entity.Expand11 = txtT_Row_5_11.Text.Trim();
                    entity.Expand12 = txtT_Row_5_12.Text.Trim();
                    entity.Expand13 = txtT_Row_5_13.Text.Trim();
                    entity.Expand14 = txtT_Row_5_14.Text.Trim();
                    entity.Expand15 = txtT_Row_5_15.Text.Trim();
                    entity.Expand16 = txtT_Row_5_16.Text.Trim();
                    entity.Expand17 = txtT_Row_5_17.Text.Trim();
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.关键资产列表.ToString("D"))
                {
                    entity.Expand1 = txtT_Row_5_18.Text.Trim();
                    entity.Expand2 = txtT_Row_5_19.Text.Trim();
                    entity.Expand3 = txtT_Row_5_20.Text.Trim();
                    entity.Expand4 = txtT_Row_5_21.Text.Trim();
                    entity.Expand5 = txtT_Row_5_22.Text.Trim();
                    entity.Expand6 = txtT_Row_5_23.Text.Trim();
                    entity.Expand7 = txtT_Row_5_24.Text.Trim();
                    entity.Expand8 = txtT_Row_5_25.Text.Trim();
                    entity.Expand9 = txtT_Row_5_26.Text.Trim();
                    entity.Expand10 = txtT_Row_5_27.Text.Trim();
                    entity.Expand11 = txtT_Row_5_28.Text.Trim();
                    entity.Expand12 = txtT_Row_5_29.Text.Trim();
                    entity.Expand13 = txtT_Row_5_30.Text.Trim();
                    entity.Expand14 = txtT_Row_5_31.Text.Trim();
                    entity.Expand15 = txtT_Row_5_32.Text.Trim();
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.其他资产列表.ToString("D"))
                {
                    entity.Expand1 = txtT_Row_11_1.Text.Trim();
                    entity.Expand2 = txtT_Row_11_2.Text.Trim();
                    entity.Expand3 = txtT_Row_11_3.Text.Trim();
                    entity.Expand4 = txtT_Row_11_4.Text.Trim();
                    entity.Expand5 = txtT_Row_11_5.Text.Trim();
                }
                else if (ddlAssetClass.SelectedValue == PubEnums.AssetType.网络设备资产.ToString("D"))
                {
                    entity.Expand1 = txtT_Row_20_1.Text.Trim();
                    entity.Expand2 = txtT_Row_20_2.Text.Trim();
                    entity.Expand3 = txtT_Row_20_3.Text.Trim();
                    entity.Expand4 = txtT_Row_20_4.Text.Trim();
                    entity.Expand5 = txtT_Row_20_5.Text.Trim();
                }


                
                //DateTime rs = new DateTime();
                //if (DateTime.TryParse(txtPurchaseDate.Text.Trim(), out  rs))
                //{
                //    entity.PurchaseDate = rs;
                //}
                //decimal purMoney = 0;
                //if (decimal.TryParse(txtPurchaseMoney.Text.Trim(), out  purMoney))
                //{
                //    entity.PurchaseMoney = purMoney;
                //}

                //entity.SalerName = txtSaler.Text.Trim();
                //entity.ServiceName = txtServiceName.Text.Trim();
                //entity.ServiceTel = txtServiceTel.Text.Trim();

                //DateTime startusingdate = new DateTime();
                //if (DateTime.TryParse(txtStartUsingDate.Text.Trim(), out  startusingdate))
                //{
                //    entity.StartUsingDate = startusingdate;
                //}

                //DateTime overusingdate = new DateTime();
                //if (DateTime.TryParse(txtOverUsingDate.Text.Trim(), out  overusingdate))
                //{
                //    entity.OverUsingDate = overusingdate;
                //}

                //entity.Version = txtVersion.Text.Trim();
                //entity.PassWord = txtPassword.Text.Trim();
                //entity.SerialNo = txtSerialNo.Text.Trim();
                //entity.AssetPosition = txtAssetPosition.Text.Trim();
                //entity.MACaddr = txtMACaddr.Text.Trim();
                //entity.HostName = txtHostName.Text.Trim();
                //entity.IPaddr1 = txtIPAddr1.Text.Trim();
                //entity.IPaddr2 = txtIPAddr2.Text.Trim();
                //entity.harddisk = txtHardDisk.Text.Trim();
                //entity.mainboard = txtmainboard.Text.Trim();
                //entity.CPU = txtCPU.Text.Trim();
                //entity.Memory = txtmemory.Text.Trim();
                //entity.OS = txtOS.Text.Trim();
                //entity.CDROM = txtCDROM.Text.Trim();
                //entity.Display = txtDisplay.Text.Trim();
                //entity.ModifyBy = SessionHelper.UserID;
                //entity.ModifyDate = DateTime.Now;


                entity.SaveOrUpdate();
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        protected void ddlAssetClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sAssetValue = this.ddlAssetClass.SelectedValue;
            if (sAssetValue=="-1")
                T_td_1.Visible = false;
            else
                T_td_1.Visible = true;

            switch (int.Parse(sAssetValue))
            {
                case (int)PubEnums.AssetType.软件资产列表:
                    this.T_Row_1.Visible = true;
                    this.T_Row_2.Visible = false;
                    this.T_Row_3.Visible = false;
                    this.T_Row_4.Visible = false;
                    this.T_Row_5.Visible = false;
                    this.T_Row_6.Visible = false;
                    this.T_Row_7.Visible = false;
                    this.T_Row_8.Visible = false;
                    this.T_Row_9.Visible = false;
                    this.T_Row_10.Visible = false;
                    this.T_Row_11.Visible = false;
                    this.T_Row_12.Visible = false;
                    this.T_Row_13.Visible = false;
                    this.T_Row_14.Visible = false;
                    this.T_Row_15.Visible = false;
                    this.T_Row_16.Visible = false;
                    this.T_Row_17.Visible = false;
                    this.T_Row_18.Visible = true;
                    this.T_Row_19.Visible = true;
                    this.T_Row_20.Visible = false;
                    this.T_Row_21.Visible = false;
                    this.T_Row_22.Visible = false;
                    break;
                case (int)PubEnums.AssetType.关键资产列表:
                    this.T_Row_1.Visible = true;
                    this.T_Row_2.Visible = false;
                    this.T_Row_3.Visible = false;
                    this.T_Row_4.Visible = false;
                    this.T_Row_5.Visible = false;
                    this.T_Row_6.Visible = false;
                    this.T_Row_7.Visible = false;
                    this.T_Row_8.Visible = false;
                    this.T_Row_9.Visible = false;
                    this.T_Row_10.Visible = false;
                    this.T_Row_11.Visible = true;
                    this.T_Row_12.Visible = true;
                    this.T_Row_13.Visible = true;
                    this.T_Row_14.Visible = true;
                    this.T_Row_15.Visible = true;
                    this.T_Row_16.Visible = false;
                    this.T_Row_17.Visible = false;
                    this.T_Row_18.Visible = false;
                    this.T_Row_19.Visible = false;
                    this.T_Row_20.Visible = false;
                    this.T_Row_21.Visible = false;
                    this.T_Row_22.Visible = false;
                    break;
                case (int)PubEnums.AssetType.电脑资产列表:
                    this.T_Row_1.Visible = true;
                    this.T_Row_2.Visible = false;
                    this.T_Row_3.Visible = false;
                    this.T_Row_4.Visible = false;
                    this.T_Row_5.Visible = true;
                    this.T_Row_6.Visible = true;
                    this.T_Row_7.Visible = true;
                    this.T_Row_8.Visible = true;
                    this.T_Row_9.Visible = true;
                    this.T_Row_10.Visible = true;
                    this.T_Row_11.Visible = false;
                    this.T_Row_12.Visible = false;
                    this.T_Row_13.Visible = false;
                    this.T_Row_14.Visible = false;
                    this.T_Row_15.Visible = false;
                    this.T_Row_16.Visible = false;
                    this.T_Row_17.Visible = false;
                    this.T_Row_18.Visible = false;
                    this.T_Row_19.Visible = false;
                    this.T_Row_20.Visible = false;
                    this.T_Row_21.Visible = false;
                    this.T_Row_22.Visible = false;
                    break;
                case (int)PubEnums.AssetType.网络设备资产:
                    this.T_Row_1.Visible = true;
                    this.T_Row_2.Visible = false;
                    this.T_Row_3.Visible = false;
                    this.T_Row_4.Visible = false;
                    this.T_Row_5.Visible = false;
                    this.T_Row_6.Visible = false;
                    this.T_Row_7.Visible = false;
                    this.T_Row_8.Visible = false;
                    this.T_Row_9.Visible = false;
                    this.T_Row_10.Visible = false;
                    this.T_Row_11.Visible = false;
                    this.T_Row_12.Visible = false;
                    this.T_Row_13.Visible = false;
                    this.T_Row_14.Visible = false;
                    this.T_Row_15.Visible = false;
                    this.T_Row_16.Visible = false;
                    this.T_Row_17.Visible = false;
                    this.T_Row_18.Visible = false;
                    this.T_Row_19.Visible = false;
                    this.T_Row_20.Visible = true;
                    this.T_Row_21.Visible = true;
                    this.T_Row_22.Visible = true;
                    break;
                case (int)PubEnums.AssetType.办公设备资产:
                    this.T_Row_1.Visible = true;
                    this.T_Row_2.Visible = true;
                    this.T_Row_3.Visible = true;
                    this.T_Row_4.Visible = true;
                    this.T_Row_5.Visible = false;
                    this.T_Row_6.Visible = false;
                    this.T_Row_7.Visible = false;
                    this.T_Row_8.Visible = false;
                    this.T_Row_9.Visible = false;
                    this.T_Row_10.Visible = false;
                    this.T_Row_11.Visible = false;
                    this.T_Row_12.Visible = false;
                    this.T_Row_13.Visible = false;
                    this.T_Row_14.Visible = false;
                    this.T_Row_15.Visible = false;
                    this.T_Row_16.Visible = false;
                    this.T_Row_17.Visible = false;
                    this.T_Row_18.Visible = false;
                    this.T_Row_19.Visible = false;
                    this.T_Row_20.Visible = false;
                    this.T_Row_21.Visible = false;
                    this.T_Row_22.Visible = false;
                    break;
                case (int)PubEnums.AssetType.其他资产列表:
                    this.T_Row_1.Visible = true;
                    this.T_Row_2.Visible = false;
                    this.T_Row_3.Visible = false;
                    this.T_Row_4.Visible = false;
                    this.T_Row_5.Visible = false;
                    this.T_Row_6.Visible = false;
                    this.T_Row_7.Visible = false;
                    this.T_Row_8.Visible = false;
                    this.T_Row_9.Visible = false;
                    this.T_Row_10.Visible = false;
                    this.T_Row_11.Visible = false;
                    this.T_Row_12.Visible = false;
                    this.T_Row_13.Visible = false;
                    this.T_Row_14.Visible = false;
                    this.T_Row_15.Visible = false;
                    this.T_Row_16.Visible = true;
                    this.T_Row_17.Visible = true;
                    this.T_Row_18.Visible = false;
                    this.T_Row_19.Visible = false;
                    this.T_Row_20.Visible = false;
                    this.T_Row_21.Visible = false;
                    this.T_Row_22.Visible = false;
                    break;
                default:
                    this.T_Row_1.Visible = false;
                    this.T_Row_2.Visible = false;
                    this.T_Row_3.Visible = false;
                    this.T_Row_4.Visible = false;
                    this.T_Row_5.Visible = false;
                    this.T_Row_6.Visible = false;
                    this.T_Row_7.Visible = false;
                    this.T_Row_8.Visible = false;
                    this.T_Row_9.Visible = false;
                    this.T_Row_10.Visible = false;
                    this.T_Row_11.Visible = false;
                    this.T_Row_12.Visible = false;
                    this.T_Row_13.Visible = false;
                    this.T_Row_14.Visible = false;
                    this.T_Row_15.Visible = false;
                    this.T_Row_16.Visible = false;
                    this.T_Row_17.Visible = false;
                    this.T_Row_18.Visible = false;
                    this.T_Row_19.Visible = false;
                    this.T_Row_20.Visible = false;
                    this.T_Row_21.Visible = false;
                    this.T_Row_22.Visible = false;
                    break;
            }
        }

    }
}
